package www.study.com;

//Pow(x, n) https://leetcode.cn/problems/powx-n/
public class code50 {
    class Solution {
        public double myPow(double x, int n) {
            //快速幂
            int f = n < 0 ? -1 : 1; //指数正负
            double res = 1;
            double base = x;
            n = Math.abs(n);
            while(n != 0){
                if((n & 1) == 1) {
                    res *= base;
                }
                base *= base;
                n >>>= 1;
            }
            return f == 1 ? res : 1.0 / res;
        }
    }
}
